//
//  AlertedMapViewController.m
//  Seizure
//
//
//  A Controller that will be presented when a push notification is recieved

#import "AlertedMapViewController.h"
#import "MapKit/MKCircle.h"
#import "EventLocationAnnotation.h"
#import "MapKit/MKPinAnnotationView.h"

@implementation AlertedMapViewController

@synthesize alertedInformation;
@synthesize mapView;
static float ALPHA = 0.25;

#pragma mark -- Map View Delegate Methods

-(MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id)overlay 
{
    MKCircleView* circleView = [[MKCircleView alloc] initWithOverlay:overlay];
    circleView.fillColor = [UIColor redColor];
    circleView.alpha = ALPHA;
    return circleView;
}

- (MKAnnotationView *)mapView:(MKMapView *)mView viewForAnnotation:(id <MKAnnotation>)annotation
{
    static NSString *AnnotationIdentifier = @"Annotation";
    MKPinAnnotationView* pinView = (MKPinAnnotationView *)[mView dequeueReusableAnnotationViewWithIdentifier:AnnotationIdentifier];
    if (!pinView)
    {
        pinView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotationIdentifier];
        pinView.pinColor = MKPinAnnotationColorGreen;
        pinView.animatesDrop = YES;
    }
    else
    {
        pinView.annotation = annotation;
    }
    return pinView;
}

#pragma mark -
#pragma mark Lifecycle events

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil dictonary:(NSDictionary *)info;
{
    self = [self initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.alertedInformation = info;
//            for (id key in alertedInformation) {
//                NSLog(@"key: %@, value: %@", key, [alertedInformation objectForKey:key]);
//            }    
    }
    return self;
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    mapView.delegate = self;
    [[self navigationController] setNavigationBarHidden:NO animated:YES];
     

//    double latitude = (double)[alertedInformation objectForKey:@"lat"];
//    double longitude = [alertedInformation objectForKey:@"long"];
//    NSLog(@"%f", [(NSString*)[alertedInformation objectForKey:@"lat"] doubleValue]);
    double latitude = [(NSString*)[alertedInformation objectForKey:@"lat"] doubleValue];
    double longitude = [(NSString*)[alertedInformation objectForKey:@"long"] doubleValue];
    double accuracy = [(NSString*)[alertedInformation objectForKey:@"accuracy"] doubleValue];
    
    //Map pin location
    CLLocationCoordinate2D coord = CLLocationCoordinate2DMake(latitude, longitude);
    EventLocationAnnotation *annotation = [[EventLocationAnnotation alloc]initWithCoordinate:coord];
    
    //Radius to highlight around the pin -- based on radius of uncertainty
    NSLog(@"Radius: %f",accuracy);
    MKCircle *radius = [MKCircle circleWithCenterCoordinate:coord radius:accuracy];
    
    //Wideness of map that will be shown
    MKCoordinateRegion region;
    region.center = coord;
    //double longitudeAlpha = 
    //Latitude delta changes by constant - 111km. horizontal accuracy is measured in meters
    MKCoordinateSpan span = {radius.radius/111000,0.01};
    region.span = span;
    
    [mapView addOverlay:radius];
    [mapView setRegion:region animated:YES];
    [mapView setMapType:MKMapTypeHybrid];
    [mapView addAnnotation:annotation];
    

}


-(void)viewWillDisappear:(BOOL)animated{
    [self.navigationController setNavigationBarHidden:YES animated:YES];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

@end
